package com.dhcc.bpm.modules.scf.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.dhcc.bpm.common.utils.StringUtils;
import com.dhcc.bpm.modules.scf.dto.ScfSalesDetailsInfoDTO;
import com.dhcc.bpm.modules.scf.entity.ScfSalesDetailsInfo;
import com.dhcc.bpm.modules.scf.service.IScfSalesDetailsInfoService;
import com.dhcc.bpm.modules.scf.vo.ScfSalesDetailsInfoVO.ScfGetSalesListVO;
import lombok.AllArgsConstructor;
import org.apache.poi.ss.formula.functions.T;
import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.tool.api.R;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * 控制器
 *
 * @author liuguoting
 * @since 2023-10-12
 */
@RestController
@AllArgsConstructor
public class ScfSalesDetailsInfoController extends BladeController {

    private IScfSalesDetailsInfoService scfSalesDetailsInfoService;

    /**
     * 销售明细信息
     */
    @GetMapping("/scfGetSalesList")
    // @Cacheable(value = "scfSalesListCache", key = "'scfSalesListCacheKey-' + #scfSalesDetailsInfo.hashCode() + '-' + #query.hashCode()", unless = "#result == null")
    public R<IPage<ScfGetSalesListVO>> page(ScfSalesDetailsInfoDTO scfSalesDetailsInfo, Query query) {
        if (StringUtils.isEmpty( scfSalesDetailsInfo.getOrgCode() ) ) {
            return  R.fail(400,"公司编码不能为空") ;
        }
        scfSalesDetailsInfo.setOrgCodes(  scfSalesDetailsInfo.getOrgCode().split(",") );

        if(scfSalesDetailsInfo.getOrgCode().contains( "50000000")){
            scfSalesDetailsInfo.setOrgCode("50000000");
            scfSalesDetailsInfo.setOrgCodes("50000000".split(",") );
        }

        IPage<ScfGetSalesListVO> pages = scfSalesDetailsInfoService.selectScfSalesDetailsInfoPage(Condition.getPage(query), scfSalesDetailsInfo);
        return R.data(pages);
    }

    /**
     * 详情
     */
    @GetMapping("/scfGetSalesDetails")
    // @Cacheable(value = "scfSalesDetailsCache", key = "'scfSalesDetailsCacheKey-' + #scfSalesDetailsInfo.rowId")
    public R<ScfSalesDetailsInfo> detail(ScfSalesDetailsInfoDTO scfSalesDetailsInfo) {
        ScfSalesDetailsInfo detail = scfSalesDetailsInfoService.getDetail(scfSalesDetailsInfo);
        return R.data(detail);
    }

    @PostMapping("/scfSalesDetailsFile")
    public R<T> isSalesDetails(String operatorId, MultipartFile file) throws IOException {
        scfSalesDetailsInfoService.isSalesDetails(operatorId, file);
        return R.success("导入成功！");
    }

    /**
     * 手动生成销售明细
     */
    @PostMapping("/sdsc")
    // @Cacheable(value = "scfSalesDetailsCache", key = "'scfSalesDetailsCacheKey-' + #scfSalesDetailsInfo.rowId")
    public void sdsc(String recyclingInNum,String orgCode) {
        scfSalesDetailsInfoService.generateSalesDetails(recyclingInNum,orgCode);
    }

}
